package srt;



import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *  srt 文件生成
 */
public class SrtGen {

    static String path="D:\\西瓜视频计划\\20221006什么是微服务\\1.txt";

    public static void main(String[] args) throws Exception{

        int i=0;
        List<String> list=new ArrayList<>();
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.HOUR,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        SimpleDateFormat fmt=new SimpleDateFormat(":mm:ss,SSS");
        for(String line:Files.readAllLines(Paths.get(path))){
            if(line==null || line.trim().length()==0){
                continue;
            }
            list.add(String.valueOf((++i)));
            String start="00"+fmt.format(cal.getTime());
            cal.add(Calendar.MILLISECOND,300*line.length());
            String end="00"+fmt.format(cal.getTime());
            list.add(start+" --> "+end);
            list.add(line);
            list.add("\n");
        }
        Files.write(Paths.get(path+".srt"),list);
        System.out.println("字幕生成完毕！！");
    }

}
